﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace UI.SellOrder
{
    public partial class FrmBack : UI.ParentForm.BaseMain
    {
        FrmBackMedit edit = null;
        ToolStripButton btnAudit = null;
        ToolStripButton btnStockIn = null;
        ToolStripButton btnPrint = null;

        public FrmBack(string modulecode, string action)
            : base(modulecode, action)
        {
            InitializeComponent();
        }

        protected override void AddToolButton()
        {
            btnAudit = new ToolStripButton("审核");
            btnAudit.Name = "_btn_p";
            btnAudit.Tag = "P";
            btnAudit.Click += new EventHandler(delegate
            {
                DataGridViewRow row = this.GetSelectedRow();
                if (null == row) return;

                OpenEdit("P", "审核", Components.Validation.ToString(row.Cells["listid"].Value));
            });
            this.tspTop.Items.Add(btnAudit);

            //入库
            btnStockIn = new ToolStripButton("退货入库");
            btnStockIn.Name = "_btn_s";
            btnStockIn.Tag = "S";
            btnStockIn.Click += new EventHandler(delegate
            {
                DataGridViewRow row = this.GetSelectedRow();
                if (null == row) return;

                OpenEdit("S", "退货入库", Components.Validation.ToString(row.Cells["listid"].Value));
            });
            this.tspTop.Items.Add(btnStockIn);

            //打印
            btnPrint = new ToolStripButton("打印退货单");
            btnPrint.Name = "_btn_R";
            btnPrint.Tag = "R";
            btnPrint.Click += new EventHandler(delegate
            {
                DataGridViewRow row = this.GetSelectedRow();
                if (null == row) return;

                var frm = new FrmOrdReturnPrint();
                frm.AddParam("listid", Components.Validation.ToString(row.Cells["listid"].Value)); //添加参数
                frm.Show();
            });
            this.tspTop.Items.Add(btnPrint);
        }

        protected override void OnAddClick(ToolStripButton btn)
        {
            edit = new FrmBackMedit(this.ModuleCode, "A", GetTitleStyle(this.Text, btn.Text), "");
            edit.ShowDialog();
            if (edit.IsReload) base.BindGrid();
        }

        protected override void OnEditClick(ToolStripButton btn, DataGridViewRow row)
        {
            OpenEdit("M", btn.Text, Components.Validation.ToString(row.Cells["listid"].Value));
        }

        protected override void OnViewClick(ToolStripButton btn, DataGridViewRow row)
        {
            OpenEdit("V", btn.Text, Components.Validation.ToString(row.Cells["listid"].Value));
        }

        private void OpenEdit(string action, string btnText, string code)
        {
            edit = new FrmBackMedit(this.ModuleCode, action, GetTitleStyle(this.Text, btnText), code);
            edit.ShowDialog();
            if (edit.IsReload) base.BindGrid();
        }

        protected override void UpdatePager(UI.Common.Pager pager)
        {
            pager.FilterAppName = "a.muser";
            pager.StrWhere = " a.status <> '9' ";
        }

        protected override void AfterRowChanged(int rowIndex)
        {
            if (rowIndex == -1 || null == btnAudit || false == btnAudit.Visible) return;

            string status = Components.Validation.ToString(this.dgvMain["audstatus", rowIndex].Value);
            
            this.tspEdit.Enabled = false;
            this.btnAudit.Enabled = false;
            this.btnStockIn.Enabled = false;
            if ("1" == status 
                || string.IsNullOrEmpty(status) == true)
            {
                this.tspEdit.Enabled = true;
                this.btnAudit.Enabled = true;
            }
            else if("2" == status || "3" == status)
            {
                this.btnStockIn.Enabled = true;
            }
        }
    }
}
